/**
 * 店铺管理API接口
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 */

import api from './request'
import type { ApiResponse } from '@/types'

// 文件信息接口（按照《用户上传文件功能使用指南.md》标准JSON格式）
export interface FileInfo {
  id?: string
  fileName?: string
  fileUrl?: string
  fileSize?: number
  uploadType?: string
  uploadSource?: string
  status?: string
  createTime?: string
  fileType?: string
  fileExtension?: string
  fileMd5?: string
  originalFileName?: string
  filePath?: string
  thumbnailUrl?: string
  description?: string
  tags?: string
  isPublic?: boolean
  downloadCount?: number
  viewCount?: number
  sortOrder?: number
  remark?: string
}

// 店铺查询参数接口
export interface ShopQueryParams {
  current?: number
  size?: number
  communityId?: number
  shopType?: number
  shopName?: string
  shopCode?: string
  buildingId?: number
  unitId?: number
  roomNumber?: string
  contactPerson?: string
  contactPhone?: string
  status?: number
  minArea?: number
  maxArea?: number
  minRentPrice?: number
  maxRentPrice?: number
}

// 店铺表单数据接口
export interface ShopFormData {
  id?: number
  shopName: string
  shopCode: string
  shopType: number
  communityId: number
  buildingId: number
  unitId: number
  roomNumber: string
  floorNumber: number
  area: number
  rentPrice: number
  contactPerson?: string
  contactPhone?: string
  businessLicense?: string
  remark?: string
  status?: number
  images?: FileInfo[] // 店铺图片信息列表（FileInfoDTO格式）
}

// 店铺详情接口
export interface ShopDetail {
  id: number
  shopName: string
  shopCode: string
  shopType: number
  shopTypeName: string
  communityId: number
  communityName: string
  buildingId: number
  buildingName: string
  unitId: number
  unitName: string
  roomNumber: string
  floorNumber: number
  area: number
  rentPrice: number
  contactPerson: string
  contactPhone: string
  businessLicense: string
  status: number
  statusName: string
  remark: string
  createTime: string
  updateTime: string
  createBy: number
  updateBy: number
  imageInfoList?: FileInfo[] // 店铺图片信息列表（FileInfoDTO格式）
  imageUrls?: string[] // 店铺图片URL列表
  firstImageUrl?: string // 第一张图片URL
  imageCount?: number // 图片数量
}

// 店铺业主关联接口
export interface ShopOwnerRelation {
  id: number
  shopId: number
  shopName: string
  ownerId: number
  ownerName: string
  ownerPhone: string
  ownerType: number
  ownerTypeName: string
  companyName?: string
  relationType: number
  relationTypeName: string
  ownershipRatio?: number
  leaseStartDate?: string
  leaseEndDate?: string
  rentPrice?: number
  depositAmount?: number
  contractNumber?: string
  contractFile?: string
  isPrimary: number
  status: number
  statusName: string
  remark?: string
  createTime: string
  updateTime: string
}

// 分页响应接口
export interface ShopPageResponse {
  records: ShopDetail[]
  total: number
  current: number
  size: number
}

/**
 * 店铺管理API
 */
export const shopApi = {
  /**
   * 分页查询店铺列表
   * @param params 查询参数
   * @returns 分页结果
   */
  getShopPage: (params: ShopQueryParams) => {
    return api.get<ApiResponse<ShopPageResponse>>('/api/region/shop/page', { params })
  },

  /**
   * 根据ID获取店铺详情
   * @param id 店铺ID
   * @returns 店铺详情
   */
  getShopDetail: (id: number) => {
    return api.get<ApiResponse<ShopDetail>>(`/api/region/shop/${id}`)
  },

  /**
   * 根据店铺ID获取业主列表
   * @param shopId 店铺ID
   * @returns 业主列表
   */
  getShopOwners: (shopId: number) => {
    return api.get<ApiResponse<ShopOwnerRelation[]>>(`/api/region/shop-owner-relation/shop/${shopId}/owners`)
  },

  /**
   * 根据店铺编码获取店铺详情
   * @param shopCode 店铺编码
   * @returns 店铺详情
   */
  getShopByCode: (shopCode: string) => {
    return api.get<ApiResponse<ShopDetail>>(`/api/region/shop/code/${shopCode}`)
  },

  /**
   * 根据社区ID获取店铺列表
   * @param communityId 社区ID
   * @returns 店铺列表
   */
  getShopsByCommunity: (communityId: number) => {
    return api.get<ApiResponse<ShopDetail[]>>(`/api/region/shop/community/${communityId}`)
  },

  /**
   * 创建店铺
   * @param data 店铺信息
   * @returns 操作结果
   */
  createShop: (data: ShopFormData) => {
    return api.post<ApiResponse<number>>('/api/region/shop', data)
  },

  /**
   * 更新店铺信息
   * @param id 店铺ID
   * @param data 店铺信息
   * @returns 操作结果
   */
  updateShop: (id: number, data: Partial<ShopFormData>) => {
    return api.put<ApiResponse<boolean>>(`/api/region/shop/${id}`, data)
  },

  /**
   * 删除店铺
   * @param id 店铺ID
   * @returns 操作结果
   */
  deleteShop: (id: number) => {
    return api.delete<ApiResponse<boolean>>(`/api/region/shop/${id}`)
  },

  /**
   * 批量删除店铺
   * @param ids 店铺ID列表
   * @returns 操作结果
   */
  batchDeleteShop: (ids: number[]) => {
    return api.delete<ApiResponse<boolean>>('/api/region/shop/batch', { data: ids })
  },

  /**
   * 启用店铺
   * @param id 店铺ID
   * @returns 操作结果
   */
  enableShop: (id: number) => {
    return api.put<ApiResponse<boolean>>(`/api/region/shop/${id}/enable`)
  },

  /**
   * 禁用店铺
   * @param id 店铺ID
   * @returns 操作结果
   */
  disableShop: (id: number) => {
    return api.put<ApiResponse<boolean>>(`/api/region/shop/${id}/disable`)
  },

  /**
   * 统计店铺数量
   * @param params 查询参数
   * @returns 统计结果
   */
  countShops: (params: { communityId?: number; shopType?: number }) => {
    return api.get<ApiResponse<number>>('/api/region/shop/count', { params })
  },

  /**
   * 检查店铺编码是否存在
   * @param shopCode 店铺编码
   * @param excludeId 排除的店铺ID
   * @returns 是否存在
   */
  checkShopCode: (shopCode: string, excludeId?: number) => {
    return api.get<ApiResponse<boolean>>(`/api/region/shop/exists/code/${shopCode}`, {
      params: { excludeId }
    })
  },

  /**
   * 更新店铺图片
   * @param id 店铺ID
   * @param imageInfoList 图片信息列表
   * @returns 操作结果
   */
  updateShopImages: (id: number, imageInfoList: any[]) => {
    return api.put<ApiResponse<boolean>>(`/api/region/shop/${id}/images`, imageInfoList)
  },

  /**
   * 获取店铺图片列表
   * @param id 店铺ID
   * @returns 图片列表
   */
  getShopImages: (id: number) => {
    return api.get<ApiResponse<any[]>>(`/api/region/shop/${id}/images`)
  }
}
